package com.ckmod.zyr3x.tools.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.ckmod.zyr3x.tools.R;
import com.ckmod.zyr3x.tools.components.CSSConfig;
import com.ckmod.zyr3x.tools.components.ComplexCheckedBilder;
import com.ckmod.zyr3x.tools.components.SeparatedListAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: zyr3x
 * Date: 12.07.11
 * Time: 13:52
 * To change this template use File | Settings | File Templates.
 */
public class Features extends Activity {

    public final static String ITEM_TITLE = "title";
    public final static String ITEM_CAPTION = "caption";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SeparatedListAdapter adapter = new SeparatedListAdapter(this);

        adapter.addSection(getString(R.string.features), simpleAdapter(getResources().getStringArray(R.array.features),
                getResources().getStringArray(R.array.features_info),
                getResources().getStringArray(R.array.features_value),
                R.layout.list_complex_checked));
        ListView list = new ListView(this);
        list.setAdapter(adapter);
        list.setOnItemClickListener(onItemClickListener());
        setContentView(list);
    }

    public SimpleAdapter simpleAdapter(String[] names, String[] infos, String[] values,int layout)
    {
        ArrayList<HashMap<String,String>> listData =  new ArrayList<HashMap<String,String>>();

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listData,
                                layout,
                                new String[] {ITEM_TITLE, ITEM_CAPTION},
                                new int[] {R.id.list_complex_title, R.id.list_complex_caption});
        simpleAdapter.setViewBinder(new ComplexCheckedBilder());

        for (int i = 0; i< names.length; i++)
        {
                    HashMap<String,String> temp = new HashMap<String,String>();
                    if(values!=null)
                        temp.put(ITEM_TITLE, names[i]+":"+ values[i]);
                    else
                        temp.put(ITEM_TITLE, names[i]);

                    if(infos !=null)
                        temp.put(ITEM_CAPTION, infos[i]);
                    else
                        temp.put(ITEM_CAPTION, "");
                    listData.add(temp);
        }

        simpleAdapter.notifyDataSetChanged();

        return simpleAdapter;
    }

    private AdapterView.OnItemClickListener onItemClickListener()
    {
      return new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        CheckedTextView v = (CheckedTextView)view.findViewById(R.id.list_complex_title);
                        boolean current = v.isChecked();
                        v.setChecked ((current == true)? false : true);
                        CSSConfig.setValue(getResources().getStringArray(R.array.features_value)[i - 1], ((current == true) ? "0" : "1"));
            }
        };
    }

    public void onBackPressed()
    {
        //CSSConfig.update();
        finish();
    }
}